package com.atguigu.gmallre.mock.bean;

import com.atguigu.gmallre.mock.constant.GmallConstant;
import com.atguigu.gmallre.mock.factory.UserSession;
import com.baomidou.mybatisplus.annotation.TableField;
import com.google.common.collect.Ordering;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Data
public class ActivityOrder {


    OrderInfo orderInfo;

    ActivityInfo activityInfo;


    @TableField(exist = false)
    private BigDecimal orderSumAmount=BigDecimal.ZERO;
    @TableField(exist = false)
    private Long orderSkuNum=0L;

    @TableField(exist = false)
    private BigDecimal reduceAmount=BigDecimal.ZERO;



    @TableField(exist = false)
    private ActivityRule bestActivityRule;



    List<OrderDetailActivity> orderDetailActivityList =new ArrayList<>();

    public void  addOrderDetail(OrderDetail orderDetail){

        OrderDetailActivity orderDetailActivity = new OrderDetailActivity();
        orderDetailActivity.setActivityId(activityInfo.getId());
        orderDetailActivity.setSkuId(orderDetail.getSkuId());
        orderDetailActivity.setOrderDetail(orderDetail);
        orderDetailActivity.setActivityOrder(this);

        orderDetailActivityList.add(orderDetailActivity);
        orderSumAmount=orderSumAmount.add(orderDetail.getSplitTotalAmount());
        orderSkuNum=orderSkuNum+orderDetail.getSkuNum() ;


    }

   // 1 通过判断 条件选出符合的条件的规则
   //2 同时计算出 每种活动能够优惠的金额
    public  boolean checkRequireCondition(){
        List<ActivityRule> activityRuleList = this.getActivityInfo().getActivityRuleList();
        if(this.getActivityInfo().getActivityType().equals(GmallConstant.ACTIVITY_RULE_TYPE_MJ)){
            for (ActivityRule activityRule : activityRuleList) {
                if(orderSumAmount.compareTo(activityRule.getConditionAmount())>0){
                    if(activityRule.getBenefitAmount().compareTo(reduceAmount)>0){
                        reduceAmount=activityRule.getBenefitAmount();
                        bestActivityRule=activityRule;
                    }
                }
            }
            return  (bestActivityRule!=null);
        }else if(this.getActivityInfo().getActivityType().equals(GmallConstant.ACTIVITY_RULE_TYPE_ML)) {

            for (ActivityRule activityRule : activityRuleList) {
                if(orderSkuNum.compareTo(activityRule.getConditionNum())>0){
                    if(activityRule.getBenefitAmount().compareTo(reduceAmount)>0){
                        reduceAmount=activityRule.getBenefitDiscount().movePointLeft(1).multiply(orderSumAmount);
                        bestActivityRule=activityRule;
                    }
                }
            }
            return  (bestActivityRule!=null);
        }else if(this.getActivityInfo().getActivityType().equals(GmallConstant.ACTIVITY_RULE_TYPE_ZK)) {

            for (ActivityRule activityRule : activityRuleList) {
                    if(activityRule.getBenefitAmount().compareTo(reduceAmount)>0){
                        reduceAmount=activityRule.getBenefitDiscount().movePointLeft(1).multiply(orderSumAmount);
                        bestActivityRule=activityRule;
                    }
            }
            return  (bestActivityRule!=null);
        }
        return  false;
    }


    public  void splitAmount(){
        List<OrderDetailActivity> orderDetailActivityList = this.orderDetailActivityList;
        BigDecimal remainReduceAmount=reduceAmount;
        for (int i = 0; i < orderDetailActivityList.size(); i++) {
            OrderDetailActivity orderDetailActivity = orderDetailActivityList.get(i);
            OrderDetail orderDetail = orderDetailActivity.getOrderDetail();
            if(i== orderDetailActivityList.size()-1 ){
                orderDetail.setSplitActivityAmount(remainReduceAmount);
                orderDetail.calcTotalAmount();
            }else{
                //  reduceDetail =  reduceALL* orderdetailTotal / orderTotal
                BigDecimal reduceDetail = reduceAmount.multiply(orderDetail.getSplitTotalAmount()).divide(orderSumAmount);
                orderDetail.setSplitActivityAmount(reduceDetail);
                remainReduceAmount=remainReduceAmount.subtract(reduceDetail);
                orderDetail.calcTotalAmount();

            }


        }


    }

    public void  syncOrderDetail(){
        for (OrderDetailActivity orderDetailActivity : orderDetailActivityList ) {
            OrderDetail orderDetail = orderDetailActivity.getOrderDetail();
            orderDetailActivity.setOrderDetailId(orderDetail.getId());
            orderDetailActivity.setOrderId(orderDetail.getOrderId());
            orderDetailActivity.setCreateTime(UserSession.getCurDateTime());
            orderDetailActivity.setActivityRuleId(this.getBestActivityRule().getId());
        }
    }

}
